﻿using DBUtility;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class DatabaseSchema
    {
        static UnitOfWork unitofwork = null;
        static Session s = null;
        public static UnitOfWork UnitOfWork
        {
            get
            {
                if (unitofwork == null||!unitofwork.IsConnected)
                {
                    unitofwork = XpoHelper.GetNewUnitOfWork();
                }
                return unitofwork;
            }
        }
        public static Session Session
        {
            get
            {
                if (s == null)
                {
                    s = XpoHelper.GetNewSession();
                }
                return s;
            }
        }
        public static void Update()
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            bool flag = false;
            bool.TryParse(cfa.AppSettings.Settings["UpdateDatabaseSchema"].Value, out flag);
            if (flag) 
            {
                XpoHelper.UpdateDatabaseSchema(GetAssembly());
                cfa.AppSettings.Settings["UpdateDatabaseSchema"].Value = false.ToString();
                cfa.Save();
                ConfigurationManager.RefreshSection("UpdateDatabaseSchema");
            }
        }

        static Assembly[] GetAssembly()
        {
            List<Assembly> assembly = new List<Assembly>();
            assembly.Add(typeof(Model.Bill.TB_AdjustPrice).Assembly);
            assembly.Add(typeof(Model.Bill.TB_AdjustPriceDetail).Assembly);
            assembly.Add(typeof(Model.Bill.TB_DailyStatement).Assembly);
            assembly.Add(typeof(Model.Bill.TB_DailyStatementDetail).Assembly);
            assembly.Add(typeof(Model.Bill.TB_Order).Assembly);
            assembly.Add(typeof(Model.Bill.TB_OrderDetail).Assembly);
            assembly.Add(typeof(Model.Bill.TB_OrderPay).Assembly);
            assembly.Add(typeof(Model.Bill.TB_StoreHouseIn).Assembly);
            assembly.Add(typeof(Model.Bill.TB_StoreHouseInDetail).Assembly);
            assembly.Add(typeof(Model.Bill.TB_StoreHouseInCost).Assembly);

            assembly.Add(typeof(Model.B_Employee).Assembly);
            assembly.Add(typeof(Model.B_Provider).Assembly);
            assembly.Add(typeof(Model.B_ProviderContact).Assembly);
            assembly.Add(typeof(Model.B_Store).Assembly);
            assembly.Add(typeof(Model.B_Warehouse).Assembly);
            assembly.Add(typeof(Model.P_Brand).Assembly);
            assembly.Add(typeof(Model.P_Category).Assembly);
            assembly.Add(typeof(Model.P_Product).Assembly);
            assembly.Add(typeof(Model.S_User).Assembly);
            assembly.Add(typeof(Model.S_UserPosSetUp).Assembly);
            assembly.Add(typeof(Model.TS_StockDetail).Assembly);

            assembly.Add(typeof(Model.S_Parameter).Assembly);
            return assembly.ToArray();
        }
    }
}
